from dataclasses import dataclass

from email_validator import EmailNotValidError, validate_email

from src.domain.common.exceptions import DomainException
from src.domain.common.value_objects import ValueObject


@dataclass(eq=False)
class WrongEmailValue(ValueError, DomainException):
    email: str


class EmptyEmail(WrongEmailValue):
    @property
    def message(self) -> str:
        return "Email can't be empty"


class WrongEmailFormat(WrongEmailValue):
    @property
    def message(self) -> str:
        return f'Wrong email format "{self.email}"'


@dataclass(frozen=True)
class UserEmail(ValueObject[str]):
    value: str

    def _validate(self) -> None:
        if len(self.value) == 0:
            raise EmptyEmail(self.value)
        try:
            validate_email(self.value)
        except EmailNotValidError:
            raise WrongEmailFormat(self.value)
